# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/2/23'
# __name__ = 'java_update.py'
from .mixin import FlowMixin
from apps.cicd.handles.base import workorder_update_pod, workorder_pod_status, workorder_rollback_pod, workorder_get_image, workorder_repo_tag_ready, workorder_git_merge, workorder_create_deployment
# Create your code here.


class UpdateFlow(FlowMixin):
    def init(self):
        '''
        初始化
        :return:
        '''
        image = workorder_get_image(self.workorder)
        self.workorder.cur_image = image
        self.workorder.save()
        step_obj = self.flow.steps.get(function='init').yes_step
        ret = self.do(step_function='%s'%(step_obj.function), action='yes')
        return ret
    def exec__update(self, action='yes'):
        '''
        确认update，包含执行update调用
        :param action:
        :return:
        '''
        if action == 'no':
            return 'finish', 'error'
        if self.workorder.nodeploy == 1:
            self._logging_warn('在环境 {} 拒绝部署'.format(self.workorder.env.name))
            return 'finish', 'success'
        if not self.workorder.yaml:
            ret = workorder_update_pod(self.workorder)
        else:
            ret = workorder_create_deployment(self.workorder)
        if not ret:
            return 'fail', 'fail'
        return 'finish', 'success'

    def exec__update_auto(self, action='yes'):
        '''
        自动执行，并完成结果查询，异步
        :return:
        '''
        if self.workorder.nodeploy == 1:
            self._logging_warn('在环境 {} 拒绝部署'.format(self.workorder.env.name))
            return 'finish', 'success'
        ret = workorder_pod_status(self.workorder)
        if ret:
            return 'finish', 'success'
        else:
            return 'fail', 'fail'
        #### 此处调用异步任务运行更新

    def exec__rollback(self, action='yes'):
        '''
        确认update，包含执行update调用
        :param action:
        :return:
        '''
        if action == 'no':
            return 'finish', 'error'
        if self.workorder.nodeploy == 1:
            self._logging_warn('在环境 {} 拒绝部署'.format(self.workorder.env.name))
            return 'finish', 'success'
        ret = workorder_rollback_pod(self.workorder)
        if not ret:
            return 'fail', 'fail'
        return 'finish', 'success'

    def exec__rollback_auto(self, action='yes'):
        '''
        自动执行，出现结果
        :param action:
        :return:
        '''
        return self.exec__update_auto()


    def start__repo_tag_ready_auto(self, action='yes'):
        '''
        repo tag 是否准备好
        :param action:
        :return:
        '''
        ret = workorder_repo_tag_ready(self.workorder)
        if ret:
            return 'finish', 'success'
        else:
            return 'fail', 'fail'
    def exec__git_merge_auto(self, action='yes'):
        '''
        自动代码合并
        :param action:
        :return:
        '''
        ret, errmsg = workorder_git_merge(self.workorder)
        if ret:
            return 'finish', 'success'
        else:
            return 'fail', 'fail'


    def exec__rollback_testing(self, action="yes"):
        return self.exec__testing(action=action)